#include "stdafx.h"

namespace cpputils
{

virtual_object::~virtual_object()
{
}

void class_factory::clear()
{
	NameMakers.clear();
	TypeNames.clear();
}

const string& class_factory::get_name(const type_info& TypeInfo) const
{
	return get_name(TypeInfo.name());
}

smart_ptr<virtual_object> class_factory::make(const string& Name) const
{
	map<string, smart_ptr<object_maker_base> > ::const_iterator It = NameMakers.find(Name) ;
	if (It == NameMakers.end()) throw runtime_error("Class name does not exist");
	return It->second->make();
}

void class_factory::add(const string& Name, const string& TypeName, smart_ptr<object_maker_base> Maker)
{
	if (NameMakers.find(Name) != NameMakers.end())
		throw runtime_error("Duplicate class names");
	if (TypeNames.find(TypeName) != TypeNames.end())
		throw runtime_error("Duplicate classes");
	NameMakers[Name] = Maker;
	TypeNames[TypeName] = Name;
}

const string& class_factory::get_name(const string& TypeName) const
{
	map<string, string>::const_iterator It = TypeNames.find(TypeName);
	if (It == TypeNames.end()) throw runtime_error("Class does not exist");
	return It->second;
}

}
